package com.dcbeida.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.adapter.GalleryAdapter;
import com.dcbeida.bean.AllShopSummary;
import com.dcbeida.bean.AllShopSummaryCurrent;
import com.dcbeida.bean.LengthObj;
import com.dcbeida.bean.ShopSummary;
import com.dcbeida.bean.ShopSummaryCurrent;
import com.dcbeida.fragment.statistics.StatisticsContract;
import com.dcbeida.fragment.statistics.StatisticsPresent;
import com.dcbeida.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class StatisticsFragment extends BaseFragment implements StatisticsContract.View {


    private StatisticsPresent mPresent;

    @Bind(R.id.tv_name)
    TextView tv_name;
    @Bind(R.id.tv_show1)
    TextView tv_show1;
    @Bind(R.id.tv_show2)
    TextView tv_show2;
    @Bind(R.id.gallery)
    Gallery gallery;
    @Bind(R.id.tv_xse)
    TextView tv_xse;
    @Bind(R.id.tv_chje)
    TextView tv_chje;
    @Bind(R.id.tv_dyxse)
    TextView tv_dyxse;
    @Bind(R.id.tv_dyjhje)
    TextView tv_dyjhje;

    @Bind(R.id.tv_dqkc)
    TextView tv_dqkc;
    @Bind(R.id.tv_ksts)
    TextView tv_ksts;
    @Bind(R.id.tv_cell1)
    TextView tv_cell1;
    @Bind(R.id.tv_cell2)
    TextView tv_cell2;
    @Bind(R.id.tv_cell3)
    TextView tv_cell3;
    @Bind(R.id.tv_cell4)
    TextView tv_cell4;
    @Bind(R.id.tv_cell5)
    TextView tv_cell5;
    @Bind(R.id.tv_current_m1)
    TextView tv_current_m1;
    @Bind(R.id.tv_current_m2)
    TextView tv_current_m2;
    @Bind(R.id.tv_other1)
    TextView tv_other1;
    @Bind(R.id.tv_other2)
    TextView tv_other2;


    private GalleryAdapter adapter;

    private int maxSales = 0;
    private int maxStock = 0;
    private List<ShopSummary.ListBean> shopSummaryList;
    private List<AllShopSummary.ListBean> shopSummaryList2;
    private List<LengthObj> list;
    private int currentPos;//当前的游标置为列表最后一个

    @Override
    public View initViews() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.home_item, null);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void initData() {
        super.initData();
        mPresent = new StatisticsPresent(this, mActivity);
        Bundle arguments = getArguments();
        if (arguments != null && arguments.containsKey("drugId")) {
            int drugId = arguments.getInt("drugId");
            mPresent.setDrugId(drugId);
        }
        if (arguments != null && arguments.containsKey("name")) {
            String name = arguments.getString("name");
            tv_name.setText(name);
        }

        if (mPresent.getDrugId() == 0) {
            tv_show1.setText(getString(R.string.xse));
            tv_show2.setText(getString(R.string.chje));
            tv_cell1.setText("元");
            tv_cell2.setText("元");
            tv_cell3.setText("元");
            tv_cell4.setText("元");
            tv_cell5.setText("元");
            tv_current_m1.setText("当月销售额");
            tv_current_m2.setText("当月进货金额");
            tv_other1.setText(getString(R.string.xse));
            tv_other2.setText(getString(R.string.chje));
        } else {
            tv_show1.setText(getString(R.string.xl));
            tv_show2.setText(getString(R.string.kc));
            tv_cell1.setText("盒");
            tv_cell2.setText("盒");
            tv_cell3.setText("盒");
            tv_cell4.setText("盒");
            tv_cell5.setText("盒");
            tv_current_m1.setText("当月销量");
            tv_current_m2.setText("当月进货");
            tv_other1.setText("销量");
            tv_other2.setText("库存");
        }

        list = new ArrayList<>();
        adapter = new GalleryAdapter(this.getActivity());

        adapter.setList(list);
        gallery.setAdapter(adapter);
        gallery.setSelection(mPresent.getMonth() - 1);
        gallery.setCallbackDuringFling(false);
        gallery.setUnselectedAlpha(0.5f); // 设置未选中图片的透明度
        currentPos = list.size() - 1;

        gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                currentPos = position;
                if (mPresent.getDrugId() == 0) {
                    AllShopSummary.ListBean bean = shopSummaryList2.get(position);
                    tv_xse.setText(String.valueOf(bean.getSalesamount()));
                    tv_chje.setText(String.valueOf(bean.getStockamount()));
                    tv_dyxse.setText(String.valueOf(bean.getSalesamount()));
                    tv_dyjhje.setText(String.valueOf(bean.getStockamount()));
                } else {
                    ShopSummary.ListBean bean = shopSummaryList.get(position);
                    tv_xse.setText(bean.getSales());
                    tv_chje.setText(bean.getStock());
                    tv_dyxse.setText(bean.getSales());
                    tv_dyjhje.setText(bean.getStockin());
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        mPresent.getShopSummaryCurrent();
        mPresent.getShopSummary();

    }

    @Override
    public void showShopSummaryCurrent(ShopSummaryCurrent data) {
        if (data == null || data.getList() == null) {
            tv_dyxse.setText("0");
            tv_dyjhje.setText("0");
            tv_dqkc.setText("0");
            tv_ksts.setText("0");
        } else if (null != data.getList() && 0 < data.getList().size()) {
            ShopSummaryCurrent.ListBean listBean = data.getList().get(0);
            tv_dyxse.setText(listBean.getSales());
            tv_dyjhje.setText(listBean.getStockin());
            tv_dqkc.setText(listBean.getStock());
            tv_ksts.setText(listBean.getDaysforsale());
        }
    }

    @Override
    public void showShopSummaryCurrent(AllShopSummaryCurrent data) {
        if (data == null || data.getList() == null) {
            tv_dyxse.setText("0");
            tv_dyjhje.setText("0");
            tv_dqkc.setText("0");
            tv_ksts.setText("0");
        } else if (null != data.getList() && 0 < data.getList().size()) {
            AllShopSummaryCurrent.ListBean listBean = data.getList().get(0);
            tv_dyxse.setText(String.valueOf(listBean.getSalesamount()));
            tv_dyjhje.setText(String.valueOf(listBean.getStockinamount()));
            tv_dqkc.setText(String.valueOf(listBean.getStockamount()));
            tv_ksts.setText(String.valueOf(listBean.getDaysforsale()));
        }
    }

    @Override
    public void showShopSummary(ShopSummary shopSummary) {

        shopSummaryList = shopSummary.getList();
        if (shopSummaryList != null) {
            for (int i = 0; i < shopSummaryList.size(); i++) {
                ShopSummary.ListBean bean = shopSummaryList.get(i);
                if (Integer.parseInt(bean.getSales()) > maxSales) {
                    maxSales = Integer.parseInt(bean.getSales());
                }
                if (Integer.parseInt(bean.getStock()) > maxStock) {
                    maxStock = Integer.parseInt(bean.getStock());
                }
            }
        }

        if (null != shopSummary.getList() && 0 < shopSummary.getList().size()) {
            if (maxSales < maxStock) {
                maxSales = maxStock;
            }
            for (int i = 0; i < shopSummary.getList().size(); i++) {
                int length = calculateMargTop((Integer.parseInt(shopSummary.getList().get(i).getSales())), maxSales);
                int length2 = calculateMargTop((Integer.parseInt(shopSummary.getList().get(i).getStock())), maxSales);
                list.add(new LengthObj(length, length2));
            }

            ShopSummary.ListBean bean = shopSummary.getList().get(shopSummary.getList().size() - 1);

//            if(mPresent.getDrugId() == 0){
//                tv_xse.setText(bean.getSales());
//                tv_chje.setText(bean.getStockin());
//            }else {
//                tv_xse.setText(bean.getSales());
//                tv_chje.setText(bean.getStock());
//            }


            tv_xse.setText(bean.getSales());
            tv_chje.setText(bean.getStock());
            gallery.setSelection(mPresent.getMonth() - 1);
            adapter.notifyDataSetChanged();
        }


    }

    @Override
    public void showAllShopSummary(AllShopSummary shopSummary) {
        shopSummaryList2 = shopSummary.getList();
        if (shopSummaryList2 != null) {
            for (int i = 0; i < shopSummaryList2.size(); i++) {
                AllShopSummary.ListBean bean = shopSummaryList2.get(i);
                if (bean.getSalesamount() > maxSales) {
                    maxSales = bean.getSalesamount();
                }
                if (bean.getStockamount() > maxStock) {
                    maxStock = bean.getStockamount();
                }
            }
        }

        if (null != shopSummary.getList() && 0 < shopSummary.getList().size()) {
            if (maxSales < maxStock) {
                maxSales = maxStock;
            }
            for (int i = 0; i < shopSummary.getList().size(); i++) {
                int length = calculateMargTop(shopSummary.getList().get(i).getSalesamount(), maxSales);
                int length2 = calculateMargTop(shopSummary.getList().get(i).getStockamount(), maxSales);
                list.add(new LengthObj(length, length2));
            }

            AllShopSummary.ListBean bean = shopSummary.getList().get(shopSummary.getList().size() - 1);
            tv_xse.setText(String.valueOf(bean.getSalesamount()));
            tv_chje.setText(String.valueOf(bean.getStockamount()));
            gallery.setSelection(mPresent.getMonth() - 1);
            adapter.notifyDataSetChanged();
        }
    }


    @Override
    public void showTip(String msg) {
        Utils.showTip(getActivity(), msg);
    }


    @Override
    public void setPresenter(StatisticsContract.Present presenter) {

    }

    private int calculateMargTop(int index, int maxL) {
        if (0 == index) {
            return Utils.convertDip2Pixel(100);//表示柱形图的头部空余位置
        } else {
            double bl = 100 * (1 - ((double) index / (double) maxL));
            if (100 == bl) {
                bl = 100;
            }

            return Utils.convertDip2Pixel((int) Math.rint(bl));//表示柱形图的头部空余位置
        }

    }

}
